package com.irdstudio.bhcredit.core.assembly.plugin.dataexport;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.irdstudio.bhcredit.core.assembly.plugin.common.PluginSysupdConfDao;

/**
 * 数据表操作类-[表名: 01.导出为文本文件插件(plugin_export_conf)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-09-16
 */
public class PluginExportConfDao {

	private static final Logger logger = LoggerFactory.getLogger(PluginSysupdConfDao.class);
	/* 连接对象 */
	Connection conn = null;	

	public PluginExportConfDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public PluginExportConf queryWithKeys(String pluginConfId,int confSort)
			throws SQLException {
		PluginExportConf dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM plugin_export_conf WHERE plugin_conf_id=?,conf_sort=?");
			ps.setString(1,pluginConfId);
			ps.setInt(2,confSort);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new PluginExportConf();
				dc.setPluginConfId(rs.getString("plugin_conf_id"));
				dc.setConfSort(rs.getInt("conf_sort"));
				dc.setExportMode(rs.getString("export_mode"));
				dc.setExportTarget(rs.getString("export_target"));
				dc.setExportToFile(rs.getString("export_to_file"));
				dc.setFileCharset(rs.getString("file_charset"));
				dc.setFieldSeparator(rs.getString("field_separator"));
				dc.setIsOkFile(rs.getString("is_ok_file"));
				dc.setOkFileFullName(rs.getString("ok_file_full_name"));
				dc.setOkFileContent(rs.getString("ok_file_content"));
				dc.setIsDealEmptyStr(rs.getString("is_deal_empty_str"));
				dc.setDealEmptyStr(rs.getString("deal_empty_str"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryPluginExportConfWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dc;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
//	public List<PluginExportConf> queryWithCond(String szCondition,String szOrderBy)
//			throws SQLException {
//		List<PluginExportConf> dcList = new ArrayList<PluginExportConf>();
//		PreparedStatement ps = null;
//		ResultSet rs = null;
//		try {
//			ps = conn.prepareStatement("SELECT * FROM plugin_export_conf "
//					+ szCondition + " " + szOrderBy);
//			rs = ps.executeQuery();
//			while (rs.next()) {
//				PluginExportConf dc = new PluginExportConf();
//				dc.setPluginConfId(rs.getString("plugin_conf_id"));
//				dc.setConfSort(rs.getInt("conf_sort"));
//				dc.setExportMode(rs.getString("export_mode"));
//				dc.setExportTarget(rs.getString("export_target"));
//				dc.setExportToFile(rs.getString("export_to_file"));
//				dc.setFileCharset(rs.getString("file_charset"));
//				dc.setFieldSeparator(rs.getString("field_separator"));
//				dc.setIsOkFile(rs.getString("is_ok_file"));
//				dc.setOkFileFullName(rs.getString("ok_file_full_name"));
//				dc.setOkFileContent(rs.getString("ok_file_content"));
//				dc.setIsDealEmptyStr(rs.getString("is_deal_empty_str"));
//				dc.setDealEmptyStr(rs.getString("deal_empty_str"));
//				dcList.add(dc);
//			}
//		} catch (SQLException e) {
//			throw new SQLException("queryPluginExportConfWithCond is Wrong!"
//					+ e.getMessage());
//		} finally {
//			close(rs, null, ps);
//		}
//		return dcList;
//	}
	
	public List<PluginExportConf> queryWithPluginConfId(String plugin_conf_id)
			throws SQLException {
		List<PluginExportConf> dcList = new ArrayList<PluginExportConf>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM plugin_export_conf WHERE plugin_conf_id=? order by conf_sort" );
			ps.setString(1,plugin_conf_id);
			rs = ps.executeQuery();
			while (rs.next()) {
				PluginExportConf dc = new PluginExportConf();
				dc.setPluginConfId(rs.getString("plugin_conf_id"));
				dc.setConfSort(rs.getInt("conf_sort"));
				dc.setExportMode(rs.getString("export_mode"));
				dc.setExportTarget(rs.getString("export_target"));
				dc.setExportToFile(rs.getString("export_to_file"));
				dc.setFileCharset(rs.getString("file_charset"));
				dc.setFieldSeparator(rs.getString("field_separator"));
				dc.setIsOkFile(rs.getString("is_ok_file"));
				dc.setOkFileFullName(rs.getString("ok_file_full_name"));
				dc.setOkFileContent(rs.getString("ok_file_content"));
				dc.setIsDealEmptyStr(rs.getString("is_deal_empty_str"));
				dc.setDealEmptyStr(rs.getString("deal_empty_str"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryPluginExportConfWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
	}

}
